using System;
using Light;
using UnityEngine;

namespace Weapons
{
    public class NewWeapon : MonoBehaviour
    {
        //[SerializeField] private int numOfAttacks; //move to WeaponDataSO

        [SerializeField] private float duration;

        [SerializeField] private AudioClip attackAudio;

        private int activeHash;

        private Animator anim;
        private int attackCounterHash;

        private OnceTimer attackCounterResetOnceTimer;
        [SerializeField] private CoreComp<AudioCom> audioManager;

        private int currentAttackCounter; //����������Ŀ�ʼ�������룬���������֮�󻹿��ܻ�������������

        private bool currentInput;
        public WeaponDataSO Data { get; private set; }

        [SerializeField]
        public int CurrentAttackCounter
        {
            get => currentAttackCounter;
            private set => currentAttackCounter = value >= Data.NumOfAttacks ? 0 : value; //MARKER
        }

        public bool CurrentInput
        {
            get => currentInput;
            set
            {
                if (currentInput != value)
                {
                    currentInput = value;
                    OnCurrentInputChange?.Invoke(currentInput);
                }
            }
        }

        public Core Core { get; private set; }

        public GameObject BaseGameObject { get; private set; }
        public GameObject WeaponGameObject { get; private set; }

        public AnimationEventHandler EventHandler { get; private set; }

        private void Awake()
        {
            //gameObject.SetActive(false);
            activeHash = Animator.StringToHash("active");
            attackCounterHash = Animator.StringToHash("counter");

            BaseGameObject = transform.Find("Base").gameObject;
            WeaponGameObject = transform.Find("WeaponSprite").gameObject;
            currentAttackCounter = 0;
            attackCounterResetOnceTimer = new OnceTimer(duration); //��ʱ�Դ��룬����ɾ��

            EventHandler = BaseGameObject.GetComponent<AnimationEventHandler>();
            anim = BaseGameObject.GetComponent<Animator>();
        }

        private void Start() //���ܻ�������⣬���ں���ִ�д����Ӱ�죬�ǵü��Ϸǿ������ж�
        {
        }

        private void Update()
        {
            attackCounterResetOnceTimer.Tick();
        }


        private void OnEnable()
        {
            //ǿ���ǿռ�����Ҫ��,ͬʱһЩC#�����ƺ�����ͨ�ã�����һ��������׼���һ������ȥ.���ַǿ��ж�������ط��в�ͨ
            if (EventHandler != null) EventHandler.OnFinish += Exit;
            if (attackCounterResetOnceTimer != null) attackCounterResetOnceTimer.OnTimerDone += ResetOnceAttackCounter;
        }

        private void OnDisable()
        {
            if (EventHandler != null) EventHandler.OnFinish -= Exit;
            if (attackCounterResetOnceTimer != null) attackCounterResetOnceTimer.OnTimerDone -= ResetOnceAttackCounter;
        }

        public event Action<bool> OnCurrentInputChange;

        public event Action OnEnter;

        public event Action OnExit;

        public void SetCurrentAttackCounter(int value)
        {
            currentAttackCounter = value;
        }

        public void SetCore(Core core)
        {
            Core = core;
            audioManager = new CoreComp<AudioCom>(Core);
        }

        public void SetData(WeaponDataSO data)
        {
            Data = data;
        }

        public void Enter()
        {
            audioManager.Comp?.PlaySFX(attackAudio);
            //gameObject.SetActive(true);
            anim.SetInteger(attackCounterHash, CurrentAttackCounter);
            anim.SetBool(activeHash, true);

            attackCounterResetOnceTimer.StartTimer();
            CurrentAttackCounter++;

            OnEnter?.Invoke();
        }

        private void Exit()
        {
            anim.SetBool(activeHash, false);

            OnExit?.Invoke();
            //gameObject.SetActive(false);
        }

        public void Initialize()
        {
            print("������ʼ�����");
        }

        public void ResetOnceAttackCounter()
        {
            CurrentAttackCounter = 0;
        }
    }
}